package com.fingard.dsp.web.runtime;

import java.util.HashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.fingard.FGBiz;
import com.fingard.dsp.web.page.PageBase;

public class RequestRecord {

	private HashMap<String,PageBase> pageMap;
	private Lock lock4map;
	public RequestRecord(){
		pageMap = new HashMap<String,PageBase>();
		lock4map = new ReentrantLock();
	}
	//private SpinLock lock = new SpinLock();
	public void add(String pReqId, PageBase pPage){
		lock4map.lock();
        try {
        	pageMap.put(pReqId, pPage);
        } catch(Throwable ex){
        	FGBiz.limitMsg.exception(this.getClass().getName(), ex);
        }finally {
        	lock4map.unlock();
        }
	}
	
	public void remove(String pReqId){
		lock4map.lock();
        try {
        	if(pageMap.containsKey(pReqId)){
        		pageMap.remove(pReqId);
        	}
        } catch(Throwable ex){
        	FGBiz.limitMsg.exception(this.getClass().getName(), ex);
        }finally {
        	lock4map.unlock();
        }
	}
	
	public PageBase getPageReq(String pReqId){
		lock4map.lock();
        try {
        	if(pageMap.containsKey(pReqId)){
    			return pageMap.get(pReqId);
    		}
        } catch(Throwable ex){
        	FGBiz.limitMsg.exception(this.getClass().getName(), ex);
        }finally {
        	lock4map.unlock();
        }
		return null;
	}
}
